﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Framework.Client;
using System.Windows.Forms;
using System.Globalization;
using Microsoft.TeamFoundation;
using System.ComponentModel;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Security.Permissions;
using System.Drawing;
using Microsoft.TeamFoundation.Client;
using System.Net.Mail;
using System.Collections;

namespace Outlook.WorkItems.Extension.Controls
{

    public partial class AlertSettings
    {
        // Fields
        private const char cAtChar = '@';
        private const string cBuildCompletedEventV1 = "BuildCompletionEvent";
        private const string cBuildCompletedEventV2 = "BuildCompletionEvent2";
        private const string cBuildCompletedFilter = "\"TeamProject[translate(text(), '{1}', '{2}') = '{0}']\" <> null";
        private const string cBuildStatusChangedEvent = "BuildStatusChangeEvent";
        private const string cBuildStatusChangedFilter = "\"TeamProject[translate(text(), '{1}', '{2}') = '{0}']\" <> null";
        private const string cCheckinEvent = "CheckinEvent";
        private const string cCheckinFilter = "\"Artifacts/Artifact[translate(@TeamProject, '{1}', '{2}') = '{0}']\" <> null";
        private const int cColAddresses = 2;
        private const int cColAlertName = 1;
        private const int cColDeliveryType = 3;
        private const int cColSubscribe = 0;
        private const char cDomainNameSeparator = '\\';
        private const char cDotChar = '.';
        private string cEmailHtml;
        private const char cEmailSplitter = ';';
        private const char cFilterSeparatorApostrophe = '\'';
        private const char cFilterSeparatorQuotes = '"';
        private const string cHelpAlertSettingsDialog = "vstf.wit.alerts.settings";
        private const string cMyBuildCompletedEventV2 = "MyBuildCompletionEvent2";
        private const string cMyBuildCompletedFilter = "\"TeamProject[translate(text(), '{3}', '{4}') = '{0}']\" <> null  AND \"RequestedFor\" = {2}{1}{2}";
        private IContainer components;
        private string cPlainText;
        private const string cWorkItemChangedEvent = "WorkItemChangedEvent";
        private const string cWorkItemChangedFilter = "\"PortfolioProject[translate(text(), '{4}', '{5}') = '{0}']\" <> null AND (\"CoreFields/StringFields/Field[ReferenceName='{6}']/OldValue\" = {2}{3}{2} OR \"CoreFields/StringFields/Field[ReferenceName='{6}']/NewValue\" = {2}{3}{2}) AND \"CoreFields/StringFields/Field[ReferenceName='System.AuthorizedAs']/NewValue\" <> {2}{1}{2}";
        //private DataGridViewCheckBoxColumn dataGridViewCheckBoxColumn1;
        //private DataGridViewComboBoxColumn dataGridViewComboBoxColumn1;
        //private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
        //private DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;
        private Button m_btnCancel;
        private Button m_btnOk;
        private string m_defaultEmailAddress;
        private bool m_dirty;
        private string m_domain;
        private DataGridView m_grid;
        private Label m_lblProj;
        private string m_projectName;
        private WorkItemStore m_store;
        private bool m_unsupported_displayname;
        private TableLayoutPanel okCancelTableLayoutPanel;
        private TableLayoutPanel overarchingTableLayoutPanel;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private DeliveryType GetDeliveryTypeForString(string input)
        {
            if (!string.Equals(input, this.cEmailHtml, StringComparison.CurrentCultureIgnoreCase) && string.Equals(input, this.cPlainText, StringComparison.CurrentCultureIgnoreCase))
            {
                return DeliveryType.EmailPlaintext;
            }
            return DeliveryType.EmailHtml;
        }

        private string GetLocalizedStringForDeliveryType(DeliveryType type)
        {
            if ((type != DeliveryType.EmailHtml) && (type == DeliveryType.EmailPlaintext))
            {
                return this.cPlainText;
            }
            return this.cEmailHtml;
        }

        private static void GetUpperToLowerTranslationStrings(string text, out string upperCase, out string lowerCase)
        {
            upperCase = string.Empty;
            lowerCase = string.Empty;
            if (!string.IsNullOrEmpty(text))
            {
                HashSet<char> set = new HashSet<char>();
                for (int i = 0; i < text.Length; i++)
                {
                    char c = char.ToUpperInvariant(text[i]);
                    if ((char.IsLetter(c) && (c != char.ToLowerInvariant(c))) && !set.Contains(c))
                    {
                        set.Add(c);
                    }
                }
                char[] array = new char[set.Count];
                int num2 = 0;
                foreach (char ch2 in set)
                {
                    array[num2++] = ch2;
                }
                Array.Sort<char>(array);
                upperCase = new string(array);
                lowerCase = upperCase.ToLowerInvariant();
            }
        }

        private string HandleNull(object input)
        {
            if (input != null)
            {
                return input.ToString();
            }
            return string.Empty;
        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(AlertSettings));
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            this.m_btnCancel = new System.Windows.Forms.Button();
            this.m_btnOk = new System.Windows.Forms.Button();
            this.m_grid = new System.Windows.Forms.DataGridView();
            this.m_lblProj = new System.Windows.Forms.Label();
            this.okCancelTableLayoutPanel = new System.Windows.Forms.TableLayoutPanel();
            this.overarchingTableLayoutPanel = new System.Windows.Forms.TableLayoutPanel();
            this.m_columnSubscribe = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            this.m_columnAlertName = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.m_columnEmail = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.m_columnFormat = new System.Windows.Forms.DataGridViewComboBoxColumn();
            this.label1 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.m_grid)).BeginInit();
            this.okCancelTableLayoutPanel.SuspendLayout();
            this.overarchingTableLayoutPanel.SuspendLayout();
            this.SuspendLayout();
            // 
            // m_btnCancel
            // 
            resources.ApplyResources(this.m_btnCancel, "m_btnCancel");
            this.m_btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.m_btnCancel.MinimumSize = new System.Drawing.Size(75, 23);
            this.m_btnCancel.Name = "m_btnCancel";
            this.m_btnCancel.Click += new System.EventHandler(this.m_btnCancel_Click);
            // 
            // m_btnOk
            // 
            resources.ApplyResources(this.m_btnOk, "m_btnOk");
            this.m_btnOk.MinimumSize = new System.Drawing.Size(75, 23);
            this.m_btnOk.Name = "m_btnOk";
            this.m_btnOk.Click += new System.EventHandler(this.m_btnOk_Click);
            // 
            // m_grid
            // 
            this.m_grid.AllowUserToAddRows = false;
            this.m_grid.AllowUserToDeleteRows = false;
            this.m_grid.AllowUserToResizeRows = false;
            this.m_grid.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
            this.m_grid.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.m_grid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.m_grid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.m_columnSubscribe,
            this.m_columnAlertName,
            this.m_columnEmail,
            this.m_columnFormat});
            resources.ApplyResources(this.m_grid, "m_grid");
            this.m_grid.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnEnter;
            this.m_grid.MultiSelect = false;
            this.m_grid.Name = "m_grid";
            this.m_grid.RowHeadersDefaultCellStyle = dataGridViewCellStyle1;
            this.m_grid.RowHeadersVisible = false;
            this.m_grid.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.m_grid.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.CellSelect;
            this.m_grid.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.m_grid_CellValueChanged);
            this.m_grid.CurrentCellDirtyStateChanged += new System.EventHandler(this.m_grid_CurrentCellDirtyStateChanged);
            // 
            // m_lblProj
            // 
            resources.ApplyResources(this.m_lblProj, "m_lblProj");
            this.m_lblProj.Name = "m_lblProj";
            // 
            // okCancelTableLayoutPanel
            // 
            resources.ApplyResources(this.okCancelTableLayoutPanel, "okCancelTableLayoutPanel");
            this.okCancelTableLayoutPanel.Controls.Add(this.m_btnOk, 0, 0);
            this.okCancelTableLayoutPanel.Controls.Add(this.m_btnCancel, 1, 0);
            this.okCancelTableLayoutPanel.Name = "okCancelTableLayoutPanel";
            // 
            // overarchingTableLayoutPanel
            // 
            resources.ApplyResources(this.overarchingTableLayoutPanel, "overarchingTableLayoutPanel");
            this.overarchingTableLayoutPanel.Controls.Add(this.m_lblProj, 0, 0);
            this.overarchingTableLayoutPanel.Controls.Add(this.m_grid, 0, 1);
            this.overarchingTableLayoutPanel.Name = "overarchingTableLayoutPanel";
            // 
            // m_columnSubscribe
            // 
            this.m_columnSubscribe.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells;
            this.m_columnSubscribe.Frozen = true;
            resources.ApplyResources(this.m_columnSubscribe, "m_columnSubscribe");
            this.m_columnSubscribe.Name = "m_columnSubscribe";
            // 
            // m_columnAlertName
            // 
            this.m_columnAlertName.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells;
            resources.ApplyResources(this.m_columnAlertName, "m_columnAlertName");
            this.m_columnAlertName.Name = "m_columnAlertName";
            this.m_columnAlertName.ReadOnly = true;
            this.m_columnAlertName.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
            // 
            // m_columnEmail
            // 
            this.m_columnEmail.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
            resources.ApplyResources(this.m_columnEmail, "m_columnEmail");
            this.m_columnEmail.Name = "m_columnEmail";
            this.m_columnEmail.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
            // 
            // m_columnFormat
            // 
            this.m_columnFormat.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells;
            this.m_columnFormat.DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.Nothing;
            resources.ApplyResources(this.m_columnFormat, "m_columnFormat");
            this.m_columnFormat.Name = "m_columnFormat";
            // 
            // label1
            // 
            resources.ApplyResources(this.label1, "label1");
            this.label1.Name = "label1";
            // 
            // AlertSettings
            // 
            this.AcceptButton = this.m_btnOk;
            resources.ApplyResources(this, "$this");
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.CancelButton = this.m_btnCancel;
            this.Controls.Add(this.label1);
            this.Controls.Add(this.overarchingTableLayoutPanel);
            this.Controls.Add(this.okCancelTableLayoutPanel);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "AlertSettings";
            this.ShowInTaskbar = false;
            ((System.ComponentModel.ISupportInitialize)(this.m_grid)).EndInit();
            this.okCancelTableLayoutPanel.ResumeLayout(false);
            this.overarchingTableLayoutPanel.ResumeLayout(false);
            this.overarchingTableLayoutPanel.PerformLayout();
            this.ResumeLayout(false);

        }

        private DataGridViewCheckBoxColumn m_columnSubscribe;
        private DataGridViewTextBoxColumn m_columnAlertName;
        private DataGridViewTextBoxColumn m_columnEmail;
        private DataGridViewComboBoxColumn m_columnFormat;
        private Label label1;
    }
}
